(function() {
  'use strict'
  
  function loadHTMLs(/*...urls*/) {
    var urls = Array.prototype.slice.call(arguments)
    
    return Promise.all(urls.map(function(url) {
      if (!_.isString(url)) {
        return Promise.resolve()
      }
      return new Promise(function(resolve, reject) {
        var link = link = document.createElement('link')
        link.rel = 'import'
        link.href = chrome.extension.getURL(url)
        link.addEventListener('load', resolve)
        link.addEventListener('error', reject)
        document.head.appendChild(link)
      })
    }))
  }
  
  function loadScripts(/*...urls*/) {
    var urls = Array.prototype.slice.call(arguments)
    
    return Promise.all(urls.map(function(url) {
      if (!_.isString(url)) {
        return Promise.resolve()
      }
      return new Promise(function(resolve, reject) {
        var script = document.createElement('script')
        script.async = false
        script.src = chrome.extension.getURL(url)
        script.addEventListener('load', resolve)
        script.addEventListener('error', reject)
        document.head.appendChild(script)
      })
    }))
  }
  
  var radio_player_html5 = document.createElement('xiami-radio'),
    radio_player_flash = document.querySelector('#radioPlayer')
  
  radio_player_flash.parentNode.replaceChild(radio_player_html5, radio_player_flash)
  
  loadScripts(
    'bower_components/webcomponentsjs/webcomponents-lite.min.js',
    'bower_components/traceur/traceur.js',
    'bower_components/es6-module-loader/dist/es6-module-loader.js'
    //'bower_components/system.js/dist/es6-module-loader.js'
  )
  .then(_.partial(loadHTMLs,
    'elements/xiami-radio.html'
  ))
  .catch(function(error) {
    console.log(error)
  })
})()